--
-- Author: ping.wang
-- Date: 2018-09-03 14:59:19
--
-- PeeragesListNodeRender 帝王之路 除了皇帝以外其他爵位的node
--

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewGuildEmperorListNode2))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )
    self._bar = self._ui.bar

    self._tips = self._ui.tips
    local buttonExplain = self._ui.Button_explain
    buttonExplain:addClickEventListener(function()
        if self._data and self._data[1] then
            local peerageId= self._data[1]
            local posTemp = buttonExplain:convertToWorldSpace(cc.p(0,0))
            executeFunc(self._onClick, "showPeerageTips", { peerageId, posTemp })
        end
    end)
    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        self:setVisible( true )
        self._ui.bg_1:setVisible(false)
        self._ui.bg_2:setVisible(false)
        self._ui.bg_3:setVisible(false)

        local peerageId = self._data[1]
        local guildInfo = self._data[2]
        local isMin = self._data[3]
        local bgType = self._data[4]
        local haveTown = self._data[5]
        if bgType == 0 then
            bgType = 3
        end
        self._ui["Panel/bg_"..bgType]:setVisible(true)


        local maxActiveTerritoryId = guildInfo:getMaxActiveTerritoryId()
        self._bar:setVisible(false)
        self._tips:setVisible(true)
        local peerageInfo = GD:queryPeerageById(tostring(peerageId))
        assert(peerageInfo ~= nil, "peerageInfo is null the id is "..peerageId)

        self._ui.name:setString(L(peerageInfo.name))
        self._ui.icon:loadTexture(GD:getImagePath( Res.guildPositionIconPath, "emperor_road_"..peerageId ))

        if peerageId <= guildInfo:getMaxActiveTerritoryId() then
            --已解锁
            self._ui.mask:setVisible(false)
            local activeTime = guildInfo:getActiveTerritoryTime(peerageId)
            local t = HistoryManager:transTime(activeTime)
            self._tips:setString(string.format(L("TimeGain"),t.y,t.m,t.d))
        else
            --未解锁
            self._ui.mask:setVisible(true)
            if haveTown and (isMin or peerageId == maxActiveTerritoryId + 1) then
                --正在努力准备解锁 需要显示进度条
                self._bar:setVisible(true)
                self._tips:setVisible(false)
                local ownExp = guildInfo:getCurTerritoryOwnExp()
                local maxExp = guildInfo:getCurTerritoryNeedExp()

                if maxExp == 0 then
                    self._bar:findChild("num"):setString("MAX")
                    self._bar:findChild("LoadingBar"):setPercent(100)
                else
                    self._bar:findChild("num"):setString(ownExp.."/"..maxExp)
                    self._bar:findChild("LoadingBar"):setPercent(ownExp/maxExp * 100)
                end
            else
                self._tips:setString(L(peerageInfo.lockDesc))
            end
        end
    else
        self:setVisible( false )
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
